/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog;

import com.googlecode.jaden.common.config.ProjectConfiguration;
import com.googlecode.jaden.common.other.Utils;
import static com.googlecode.jaden.ui.Constants.*;
import com.googlecode.jaden.ui.UI;
import com.googlecode.jaden.ui.UiUtils;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.prefs.Preferences;

public class EditProjectDetailsDialog extends BaseDialog {

    private final UI ui;
    private final JTextField nameField;
    private final JTextArea descriptionArea;

    public EditProjectDetailsDialog(UI ui, Preferences preferences, ProjectConfiguration project, boolean initial) {
        super(ui, DIALOG_PROJECT_DETAILS_TITLE);
        this.ui = ui;
        nameField = new JTextField(20);
        nameField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                enableOkButton();
            }
        });
        descriptionArea = new JTextArea(6, 20);
        nameField.setText(project.getName());
        descriptionArea.setText(project.getDescription());
        JLabel nameLabel = new JLabel(Utils.translate(LABEL_NAME_TEXT));
        if (initial) {
            nameField.selectAll();
        }
        JLabel descriptionLabel = new JLabel(Utils.translate(LABEL_DESCRIPTION_TEXT));
        UiUtils.enWidth(nameLabel, descriptionLabel);
        JPanel mainPanel = UiUtils.verticalList(UiUtils.horizontalList(nameLabel, nameField),
                UiUtils.horizontalList(descriptionLabel, new JScrollPane(descriptionArea)));
        setMainPanel(mainPanel, preferences);

        enableOkButton();
    }

    private void enableOkButton() {
        okButton.setEnabled(nameField.getText().trim().length() > 0);
    }

    protected void doOk() {
        ui.updateProjectDetails(nameField.getText(), descriptionArea.getText());
        dispose();
    }

}
